package com.meilai.project.controller.web.workPlatform.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.meilai.project.controller.web.workPlatform.vo.CalendarEventVO;
import com.meilai.project.entity.business.customer.CustomerInfo;
import com.meilai.project.service.business.customer.CustomerInfoService;
import com.meilai.project.util.ThreadLocalUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 张驰
 * @description
 * @date 2022-09-07 11:39
 */
@Service
public class WorkPlatformServiceImpl implements WorkPlatformService{
    @Autowired
    private CustomerInfoService customerInfoService;

    @Override
    public List<CalendarEventVO> calendarEventList() {
        List<CalendarEventVO> result = new ArrayList<>();

        Long currentUserId = ThreadLocalUserUtil.get().getId();

        // 限制拉取前3个月开始的所有事件
        LocalDateTime begin = LocalDateTime.now().minusMonths(3L);

        // 客户跟进事件
        List<CustomerInfo> customers = customerInfoService.list(Wrappers.lambdaQuery(CustomerInfo.class)
                .isNull(CustomerInfo::getDeleted_at)
                .isNotNull(CustomerInfo::getNext_visit_time)
                .ge(CustomerInfo::getNext_visit_time, begin)
                .eq(CustomerInfo::getMaintenance_staff_id, currentUserId));
        result.addAll(CalendarEventVO.genCustomerFollowEvent(currentUserId, customers));

        return result;
    }
}
